package com.ly.mpayment.persist.service.downshopinfo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.*;
import com.ly.mpayment.persist.service.debitlog.IDebitLogService;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
import com.ly.mpayment.persist.service.downshoprechargeservice.IDownShopRechargeService;
import com.ly.mpayment.persist.service.platformaccount.IPlatformAccountService;
import com.ly.mpayment.persist.service.platformaccountrecord.IPlatFormAccountRecordService;
import com.ly.mpayment.persist.service.user.IUserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.util.CollectionUtils;

@Service
public class DownShopInfoServiceImpl extends AbstractService<DownShopInfo, Serializable>
		implements IDownShopInfoService {

	Logger logger = Logger.getLogger(DownShopInfoServiceImpl.class);
	@Autowired
	IUserService userService;
	@Autowired
	IDebitLogService logService;
	@Autowired
	IPlatformAccountService accountService;

	@Autowired
	IDownShopRechargeService rechargeService;

	@Autowired
	IPlatFormAccountRecordService recordService;

	@Override
	public boolean addDownShopInfo(DownShopInfo info) {
		if (info != null) {
			this.saveOrUpdate(info);
			return true;
		}
		return false;
	}

	@Override
	public DownShopInfo findById(String id) {
		return this.findUnique("select d from DownShopInfo d where d.downshopuuid = ? ", id);
	}


	@Override
	public DownShopInfo findByIdSql(String id) {
//		Object obj [] = (Object[]) this.getSession().createSQLQuery("select d.downshopuuid,d.downshopbalance from downshopinfo d where d.downshopuuid = '"+id+"' for update").list().get(0);
//		DownShopInfo info = new DownShopInfo();
//		info.setDownshopuuid(obj[0].toString());
//		info.setDownshopbalance(BigUtil.getBigDecimal(obj[1].toString()));
//		return info;
		return this.findUnique("select d from DownShopInfo d where d.downshopuuid = ? ", id);
	}

	@Override
	public Page<DownShopInfo> findPageDownShopInfo(Map<String, Object> parameters, int start, int pageSize) {
		return this.findPageByNamed(
				"select d from DownShopInfo d left join fetch d.user u where u.usertype = 3 #[and u.useruuid = :useruuid]#  #[and u.username like :username]# #[and u.userstatus = :userstatus]#  #[and d.downshopnum = :phonenum]#  order by u.username",
				parameters, start, pageSize);
	}

	@Override
	public boolean updateDownShopInfoSubtract(DownShopInfo shop,DownRechargeFlow flow) {
		if (shop != null && flow != null) {
			shop = this.findById(shop.getDownshopuuid());
			//充值时减去渠道商金额
			shop.setDownshopbalance(shop.getDownshopbalance().subtract(flow.getDiscountAmount()));

			this.updateEntity(shop);

			logger.info("扣款下游订单:"+flow.getDiscountAmount()+",phone:"+flow.getDowonphonenum());
			//保存扣款记录
			DebitLog log = new DebitLog();
			log.setDebitTime(DateUtils.getNowDate());
			log.setDebitAmount(flow.getDiscountAmount());
			log.setDebitStatus(Config.debitSuccess);
			log.setDownShopInfo(shop);
			log.setDownOrder(flow);
			log.setStatus(Config.publicStatus[1]);
			log.setBalance(shop.getDownshopbalance());
			logService.saveDebitLog(log);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateDownShopInfoAdd(DownShopInfo shop,DownRechargeFlow flow) {
		if (shop != null && flow != null) {
			shop = this.findById(shop.getDownshopuuid());
			//充值时减去渠道商金额
			shop.setDownshopbalance(shop.getDownshopbalance().add(flow.getDiscountAmount()));

			this.updateEntity(shop);
			return true;
		}
		return false;
	}



	@Override
	public  boolean addDownShopInfo(DownShopInfo info, User user) {
		if (info != null && user != null) {
			user = userService.addUserShop(user);
			info.setUser(user);
			this.addDownShopInfo(info);
			return true;
		}
		return false;
	}

	@Override
	public DownShopInfo findShopNum(String shopNum) {
		return this.findUnique("select p from DownShopInfo p where p.downshopnum = ?", shopNum);
	}

	@Override
	public DownShopInfo findShopIp(String ip) {
		return this.findUnique("select p from DownShopInfo p where p.downshopip = ?", ip);
	}

//	@Override
//	public synchronized boolean updateShopAmount(DownShopInfo info, User userAdmin, BigDecimal big, String remark, int paytype) {
//		if (info != null) {
//			info = this.findById(info.getDownshopuuid());
//
//			DownShopRecharge recharge = new DownShopRecharge();
//			PlatFormAccount account = accountService.findByAccount();
//			if (account != null) {
//				if (paytype == Config.RECHARGE[0]) {
//					info.setDownshopbalance(info.getDownshopbalance().add(big));
//					account.setPlatformamount(account.getPlatformamount().add(big));
//				} else {
//					// 扣款
//					info.setDownshopbalance(info.getDownshopbalance().subtract(big));
//					account.setPlatformamount(account.getPlatformamount().subtract(big));
//				}
//				this.saveOrUpdate(info);
//				accountService.updatePlatformaccount(account);
//				recharge.setPlatFormAccount(account);
//			} else {
//				// 如果为null 则初始化一条
//				account = accountService.addPlatformaccount();
//				if (paytype == Config.RECHARGE[0]) {
//					info.setDownshopbalance(info.getDownshopbalance().add(big));
//					account.setPlatformamount(account.getPlatformamount().add(big));
//				} else {
//					// 扣款
//					info.setDownshopbalance(info.getDownshopbalance().subtract(big));
//					account.setPlatformamount(account.getPlatformamount().subtract(big));
//				}
//				this.saveOrUpdate(info);
//				accountService.updatePlatformaccount(account);
//				recharge.setPlatFormAccount(account);
//			}
//
//			PlatFormAccountRecord record = new PlatFormAccountRecord();
//			record.setChangeamount(paytype == Config.RECHARGE[0] ? big : big.multiply(new BigDecimal(-1)));
//			record.setChangetime(DateUtils.getNowDate());
//			record.setPlatFormAccount(account);
//			record.setRemark(paytype == 1 ? Config.rechargeDown[0] : Config.rechargeDown[1]);
//			record.setUser(userAdmin);
//			record.setPayType(paytype);
//			recordService.addPlatformaccountrecord(record);
//
//			// 充值所属用户
//			recharge.setUser(info.getUser());
//			recharge.setRechargetime(DateUtils.getNowDate());
//			recharge.setRechargestatus(Config.publicStatus[1]);
//			recharge.setRechargeamount(paytype == Config.RECHARGE[0] ? big : big.multiply(new BigDecimal(-1)));
//			recharge.setRemark(remark);
//			recharge.setPayType(paytype);
//			rechargeService.addDownShopRecharge(recharge);
//			return true;
//		}
//		return false;
//	}

	@Override
	public DownShopInfo findShopInfoByUuid(String userUuid) {
		return this.findUnique(
				"select p from DownShopInfo p left join fetch p.user left join fetch p.provincialInformation where ( p.user.userstatus = 1 or p.user.userstatus = 2 or p.user.userstatus = 3 ) and p.user.usertype = 3 and p.user.useruuid =?",
				userUuid);
	}

	@Override
	public DownShopInfo findOneBroadShop(String userUuid) {
		return this.findUnique(
				"select p from DownShopInfo p left join fetch p.user left join fetch p.provincialInformation where ( p.user.userstatus = 1 or p.user.userstatus = 2 or p.user.userstatus = 3 ) and p.user.usertype = 5 and p.user.useruuid =?",
				userUuid);
	}


	@Override
	public List<DownShopInfo> findShopList(String province) {
		return this.find(
				"select d from DownShopInfo d left join fetch d.user u where u.usertype = 3 and d.provincialInformation.provinceuuid = ?",
				province);
	}

	@Override
	public List<DownShopInfo> findShopList(Map<String, Object> parameters) {
		//
		return this.findNameAll(
				"select d from DownShopInfo d left join fetch d.user u where u.usertype = 3  #[ and u.useruuid = :useruuid ]#",
				parameters);
	}


	@Override
	public DownShopInfo findByCallUrl(String partnerid) {
		return this.findUnique(
				"select p from DownShopInfo p left join fetch p.user left join fetch p.provincialInformation where p.user.userstatus != 0 and p.user.usertype = 3 and p.orderCallAddress =?",
				partnerid);
	}

	/**
	 * 充值订单检查用户 查询用户是否存在，正常，包含 正常和维护的下游用户
	 */
	@Override
	public DownShopInfo findShopInfoForMaintenance(String userUuid) {
		return this.findUnique(
				"select p from DownShopInfo p left join fetch p.user left join fetch p.provincialInformation where p.user.userstatus != 0 and p.user.usertype = 3 and p.user.useruuid =?",
				userUuid);
	}

	@Override
	public DownShopInfo findShopInfoByuseruuid(String userUuid) {
		return this.findUnique("select d from DownShopInfo d where d.user.useruuid = ?", userUuid);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String findDownBill(Map<String, Object> parameters, int start, int pageSize) {
		Page page = new Page();
		page.setStart(start);
		page.setPageSize(pageSize);
		JSONArray arrays = JsonUtils.getJSONARRAY();
		JSONObject json = JsonUtils.getJSONOBJCT();
		String sql = "select  (d.downshopbalance + ((select case when sum(df.discount_amount) is null then 0 else sum(df.discount_amount) end from down_order df where df.useruuid = d.useruuid ";

		if (parameters.containsKey("endtime")) {
			sql += " and DATE_FORMAT(df.createtime,'%Y-%m-%d')  > '" + parameters.get("endtime").toString() + "' ";
		}
		sql += " and df.`status` = 2)  ";

		sql += " +(select case when sum(df.discount_amount) is null then 0 else sum(df.discount_amount) end from down_order df where df.useruuid = d.useruuid  ";
		if (parameters.containsKey("endtime")) {
			sql += " and DATE_FORMAT(df.createtime,'%Y-%m-%d')  > '" + parameters.get("endtime").toString() + "' ";
		}
		sql += " and df.`status` = 1))  ";


		sql += " ) - (select case  when  sum(dd.rechargeamount) is null then 0 else sum(dd.rechargeamount) end from downshoprecharge dd where dd.useruuid = d.useruuid ";
		if (parameters.containsKey("endtime")) {
			sql += " and DATE_FORMAT(dd.createtime,'%Y-%m-%d')  > '" + parameters.get("endtime").toString() + "' ";
		}

		sql += ") as 'count'  ,u.username,u.phonenum ,(select case  when  sum(dp.rechargeamount) is null then 0 else sum(dp.rechargeamount) end  from downshoprecharge dp where dp.useruuid = d.useruuid ";
//		 > '2018-09-30'
		if (parameters.containsKey("starttime")) {
			sql += "  and DATE_FORMAT(dp.createtime ,'%Y-%m-%d') >= '" + parameters.get("starttime").toString() + "' ";
		}

		if (parameters.containsKey("endtime")) {
			sql += "  and DATE_FORMAT(dp.createtime ,'%Y-%m-%d') <= '" + parameters.get("endtime").toString() + "' ";
		}
		sql += " ) as 'add' from downshopinfo d,user u where u.useruuid = d.useruuid and d.status = 1 ";
		if (parameters.containsKey("useruuid")) {
			sql += " and d.useruuid = '" + parameters.get("useruuid").toString() + "' ";
		}

		List lists = PaginationUtil.queryPaginationSQL("select * from (" + sql + ") temps", page, this.getSession())
				.getResult();
		if (lists != null && !lists.isEmpty()) {
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("count", objs[0] == null ? "0" : new BigDecimal(objs[0].toString()).setScale(5));
				obj.put("username", objs[1] == null ? "" : objs[1].toString());
				obj.put("account", objs[2] == null ? "" : objs[2].toString());
				obj.put("addbill", objs[3] == null ? "" : objs[3].toString());
				arrays.add(obj);
			}
		}
		json.put("result", arrays);
		json.put("totalCount", page.getTotalCount());
		return json.toString();
	}


	@Override
	public Page<DownShopInfo> findBroadShopPage(Map<String, Object> parameters, int start, int pageSize) {
		return this.findPageByNamed(
				"select d from DownShopInfo d left join fetch d.user u where u.usertype = 5 #[and u.useruuid = :useruuid]#  #[and u.username like :username]# order by u.username",
				parameters, start, pageSize);
	}


	@Override
	public boolean updateDownShopInfoDownKey(DownShopInfo shop) {
		DownShopInfo shopOld = this.findById(shop.getDownshopuuid());
		if(shopOld != null){
			shopOld.setDownkey(shop.getDownkey());
			this.updateEntity(shopOld);
			return true;
		}
		return false;
	}

	/**
	 * type 1订单充值扣款 2退款 3充值
	 */
	@Override
	public  boolean updateDownShopInfo(DownRechargeFlow flow, DownShopInfo shop, BigDecimal big,String remark,int paytype,User userAdmin) {
		if (shop != null) {
			shop = this.findById(shop.getDownshopuuid());

			DownShopRecharge recharge = new DownShopRecharge();
			PlatFormAccount account = accountService.findByAccount();
			if (account != null) {
				if (paytype == Config.RECHARGE[0]) {
					shop.setDownshopbalance(shop.getDownshopbalance().add(big));
					account.setPlatformamount(account.getPlatformamount().add(big));
					shop.setTotalMoney(shop.getTotalMoney().add(big));
				} else {
					// 扣款
					shop.setDownshopbalance(shop.getDownshopbalance().subtract(big));
					account.setPlatformamount(account.getPlatformamount().subtract(big));
					shop.setTotalMoney(shop.getTotalMoney().subtract(big));
				}
				this.updateEntity(shop);

				accountService.updatePlatformaccount(account);
				recharge.setPlatFormAccount(account);

			} else {
				// 如果为null 则初始化一条
				account = accountService.addPlatformaccount();
				if (paytype == Config.RECHARGE[0]) {
					shop.setDownshopbalance(shop.getDownshopbalance().add(big));
					account.setPlatformamount(account.getPlatformamount().add(big));
				} else {
					// 扣款
					shop.setDownshopbalance(shop.getDownshopbalance().subtract(big));
					account.setPlatformamount(account.getPlatformamount().subtract(big));
				}

				this.updateEntity(shop);
				accountService.updatePlatformaccount(account);
				recharge.setPlatFormAccount(account);
			}

			PlatFormAccountRecord record = new PlatFormAccountRecord();
			record.setChangeamount(paytype == Config.RECHARGE[0] ? big : big.multiply(new BigDecimal(-1)));
			record.setChangetime(DateUtils.getNowDate());
			record.setPlatFormAccount(account);
			record.setRemark(paytype == 1 ? Config.rechargeDown[0] : Config.rechargeDown[1]);
			record.setUser(userAdmin);
			record.setPayType(paytype);
			recordService.addPlatformaccountrecord(record);

			// 充值所属用户
			recharge.setUser(shop.getUser());
			recharge.setRechargetime(DateUtils.getNowDate());
			recharge.setRechargestatus(Config.publicStatus[1]);
			recharge.setRechargeamount(paytype == Config.RECHARGE[0] ? big : big.multiply(new BigDecimal(-1)));
			recharge.setRemark(remark);
			recharge.setPayType(paytype);
			recharge.setBalance(shop.getDownshopbalance());
			rechargeService.addDownShopRecharge(recharge);
			return true;
		}

		return false;
	}

	/**
	 * 余额不足通知
	 * @return
	 */
	@Override
	public List<JSONObject> findDownshopBalance() {
		List<JSONObject> objectList = new ArrayList<>();
		String sql = " SELECT u.username,d.downshopbalance from user u" +
				" inner join downshopinfo d on d.useruuid = u.useruuid" +
				" where u.userstatus = 1 and u.usertype = 3 and d.notice_switch = 1 and d.downshopbalance < d.notice_balance ";
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("downshopbalance", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}

	@Override
	public List<JSONObject> findUpshopBalance() {
		List<JSONObject> objectList = new ArrayList<>();
		String sql = " SELECT u.username,d.upshopbalance from user u" +
				" inner join upshopinfo d on d.useruuid = u.useruuid" +
				" where u.userstatus = 1 and u.usertype = 4 and d.notice_switch = 1 and d.upshopbalance < d.notice_balance ";
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("upshopbalance", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}

	@Override
	public List<JSONObject> findMerchantcredit() {
		List<JSONObject> objectList = new ArrayList<>();
		String sql = " SELECT u.username,d.downshopbalance from user u" +
				" inner join downshopinfo d on d.useruuid = u.useruuid" +
				" where u.userstatus = 1 and u.usertype = 3 and d.notice_switch = 1 and d.downshopbalance < "+ CookieRedis.get("merchantcredit");
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("downshopbalance", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}


	@Override
	public List<JSONObject> downArtificial() {
		List<JSONObject> objectList = new ArrayList<>();
		String sql = " select u.username,count(d.downorderuuid) from down_order d" +
				" INNER JOIN user u on u.useruuid = d.useruuid" +
				" where d.`status` = 11   GROUP BY u.username ";
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("counts", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}

	@Override
	public List<JSONObject> upArtificial() {
		List<JSONObject> objectList = new ArrayList<>();
		String sql = " select u.username,count(d.uporderuuid) from uporder d" +
				" INNER JOIN user u on u.useruuid = d.useruuid" +
				" where d.`status` = 11   GROUP BY u.username  ";
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("counts", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}

	@Override
	public List<JSONObject> downStreamcard() {
		String endTime = DateUtils.getNowDateToString();
		String startTime = DateUtils.toLongDate(DateUtils.addMinutes(Integer.valueOf("-"+CookieRedis.get("down_order_artificial")),DateUtils.parseDate(endTime)));

		List<JSONObject> objectList = new ArrayList<>();
		String sql = " select u.username,count(d.uporderuuid) from uporder d" +
				" inner join user u on u.useruuid = d.useruuid" +
				" where d.`status` = 1 and d.createtime < '"+startTime+"' GROUP BY u.username ";
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("counts", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}

	@Override
	public List<JSONObject> upOrderMinute() {
		String endTime = DateUtils.getNowDateToString();
		String startTime = DateUtils.toLongDate(DateUtils.addMinutes(Integer.valueOf("-"+CookieRedis.get("continuity_failt")),DateUtils.parseDate(endTime)));

		List<JSONObject> objectList = new ArrayList<>();
		String sql = " select username,(sumorder = failtcount) failt from (" +
				" select count(d.downorderuuid) sumorder,u.username,(select count(u.uporderuuid) from uporder u where u.useruuid = d.useruuid and u.`status` = 3" +
				" and u.createtime >= '"+startTime+"' and u.createtime <= '"+endTime+"') failtcount from uporder d" +
				" inner join user u on u.useruuid = d.useruuid" +
				" where d.createtime >= '"+startTime+"' and d.createtime <= '"+endTime+"' GROUP BY u.username) tt" +
				" where sumorder = failtcount ";
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				JSONObject obj = JsonUtils.getJSONOBJCT();
				Object[] objs = (Object[]) lists.get(i);
				obj.put("username", objs[0] == null ? "" : objs[0].toString());
				obj.put("counts", objs[1] == null ? "" : objs[1].toString());
				objectList.add(obj);
			}
		}
		return objectList;
	}

	@Override
	public JSONObject findByBillUserId(String useruuid,String startTimex,String endtTimex) {
		String startTime = startTimex + " 00:00:00";
		String endTime = endtTimex  + " 23:59:59";

		String sql = " select" +
				" (SELECT count(fl.downorderuuid) FROM down_order fl where fl.useruuid = d.useruuid and fl.`status` = 2 and" +
				" fl.createtime >= '" + startTime+ "' and" +
				" fl.createtime  <'"+endTime+"'   ) '订单数'," +
				" d.useruuid,u.username," +
				" (SELECT sum(fl.discount_amount) FROM down_order fl where fl.useruuid = d.useruuid and fl.`status` = 2 and" +
				" fl.createtime >= '"+startTime+"' and" +
				" fl.createtime  <'"+endTime+"'   ) 'xh'," +
				" (SELECT sum(fl.rechargeamount) FROM down_order fl where fl.useruuid = d.useruuid and fl.`status` = 2 and" +
				" fl.createtime >= '"+startTime+"' and" +
				" fl.createtime  <'"+endTime+"'   ) 'mz'," +
				" (SELECT IFNULL(sum(c.rechargeamount),0) from downshoprecharge c where c.useruuid = u.useruuid and " +
				" c.createtime >= '"+startTime+"' and c.createtime < '"+endTime+"') 'jk'," +
				" (select f.downshopbalance from downshopinfo f where f.useruuid = u.useruuid)'ye'," +
				" (select IFNULL(f.balance,0) from zero_balance f where f.user_id = u.useruuid and f.create_time = '"+startTime+"')'sq'" +
				" from downshopinfo d" +
				" inner join user u on u.useruuid = d.useruuid" +
				" where    d.useruuid = '"+useruuid+"'" +
				" GROUP BY d.useruuid  ";
		JSONObject obj = JsonUtils.getJSONOBJCT();
		List lists = this.getSession().createSQLQuery(sql).list();
		if(!CollectionUtils.isEmpty(lists)){
			for (int i = 0; i < lists.size(); i++) {
				Object[] objs = (Object[]) lists.get(i);
				obj.put("orderNum", objs[0] == null ? "" : objs[0].toString());
				obj.put("username", objs[2] == null ? "" : objs[2].toString());
				obj.put("xh", objs[3] == null ? "" : objs[3].toString());
				obj.put("mz", objs[4] == null ? "" : objs[4].toString());
				obj.put("jkje", objs[5] == null ? "" : objs[5].toString());
				obj.put("dqye", objs[6] == null ? "" : objs[6].toString());
				obj.put("sqye", objs[7] == null ? "" : objs[7].toString());
			}
		}
		return obj;
	}

	@Override
	public boolean noticeDownSwitch(JSONObject json) {
		DownShopInfo shop = findById(json.getString("downshopuuid"));
		if(shop == null){
			return false;
		}
		shop.setNoticeSwitch(json.getBoolean("noticeSwitch"));
		this.updateEntity(shop);
		return true;
	}
}
